package com.bawei.cashier

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.PayerEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.cashier.databinding.ActivityCashierBinding
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/cashier/CashierActivity")
class CashierActivity : BaseActivity<ActivityCashierBinding, CashierViewModel>() {
    lateinit var cashierAdapter: CashierAdapter
    var listPayer = mutableListOf<PayerEntity>()
    override fun initView() {
        viewModel.payerById(9)
        cashierAdapter = CashierAdapter()
        binding.vashierRec.adapter = cashierAdapter
        binding.vashierRec.layoutManager = LinearLayoutManager(this)
        binding.payerMonth.setOnClickListener {
            val arrayListOf =
                arrayListOf("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
            val optionsPickerBuilder = OptionsPickerBuilder(
                this
            ) { options1, _, _, _ ->




                binding.payerMonthTv.setTextColor(Color.BLUE)
                binding.payerYearTv.setTextColor(Color.GRAY)
                binding.payerMonthTv.text = arrayListOf[options1]

                cashierAdapter.data.clear()
                listPayer.forEach {
                    if (it.paymentTime != null)
                        if (it.paymentTime.toString()
                                .substring(5, 7) == arrayListOf[options1]
                        ) {
                            cashierAdapter.data.add(it)
                        }
                }
                cashierAdapter.notifyDataSetChanged()
            }.build<String>()
            optionsPickerBuilder.setPicker(arrayListOf)
            optionsPickerBuilder.show()
        }
        binding.payerYear.setOnClickListener {
            binding.payerMonthTv.setTextColor(Color.GRAY)
            binding.payerYearTv.setTextColor(Color.BLUE)
            cashierAdapter.data.clear()
            cashierAdapter.data.addAll(listPayer)
            cashierAdapter.notifyDataSetChanged()
            SimpleDateFormat()
        }
        cashierAdapter.setOnItemClickListener { _, _, position ->
            ARouter.getInstance().build("/cashier/CashierDetailActivity").withInt("id",cashierAdapter.data[position].id).navigation()
        }
    }

    override fun initData() {
        viewModel.payerSuccess.observe(this) {
            listPayer.clear()
            listPayer.addAll(it)
            cashierAdapter.data.clear()
            cashierAdapter.data.addAll(it)
            cashierAdapter.notifyDataSetChanged()
        }
    }
}